//  @ Project : GagalPanen
//  @ File Name : itemslot.cpp
//  @ Date : 3/9/2011
//  @ Author : Denis
//
//

#include <cstdio>
#include "itemslot.hpp"
#include "seed.hpp"
#include "fruit.hpp"

ItemSlot::ItemSlot(GameInterface* game, Item* original_item) :
    game_(game), items_() {
  add(original_item);
}

ItemSlot::ItemSlot(GameInterface* game, std::ifstream& is) :
    game_(game), items_() {
  int n;
  is >> n;
  for (int i = 0; i < n; ++i) {
    std::string buffer;
    is >> buffer;
    if (buffer == "SEED") {
      items_.push_back(new Seed(game_, is));
    } else if (buffer == "FRUIT") {
      items_.push_back(new Fruit(game_, is));
    } else {
      assert(false);
    }
  }
}

void ItemSlot::save(std::ofstream& os) {
  os << items_.size() << std::endl;
  for (int i = 0; i < (int)items_.size(); ++i) {
    if (items_[i]->getType() == kSeed) {
      os << "SEED" << std::endl;
    } else if (items_[i]->getType() == kFruit) {
      os << "FRUIT" << std::endl;
    } else {
      assert(false);
    }
    items_[i]->save(os);
  }
}

ItemSlot::~ItemSlot() {
}

void ItemSlot::add(Item* item) {
  items_.push_back(item);
}

Item* ItemSlot::getItem() {
  return items_[items_.size()-1];
}
int ItemSlot::numberOfItems(){
  return items_.size();
}

void ItemSlot::removeItem(Item* item) {
  for (int i = 0; i < (int)items_.size(); ++i) {
    if (items_[i] == item) {
      items_.erase(items_.begin() + i);
      return;
    }
  }
  return;
}

bool ItemSlot::contains(Item* item) {
  for (int i = 0; i < (int)items_.size(); ++i) {
    if (items_[i] == item) {
      return true;
    }
  }
  return false;
}

